<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>骡窝日报详情</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/newsContent.css">
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="js/common.js"></script>

    <script>
        $(function () {
            var param = getParams();
            $.get("/news/" + param.id, function (data) {
                $(".newsDetail").renderValues(data, {
                    getHref: function (item, value) {
                        var href = $(item).data("href");
                        $(item).attr("href", href + value);
                    }
                })
            })


            var num ;//总数
            var state;
            //收藏数量
            $.get("/news/nums",{newsId:param.id,type:3},function (data) {
                num = data;
                $("#collectBtn").html(num)
            })

            var num1 ;//总数
            var state1;
            //点赞数量
            $.get("/news/praiseNum",{newsId:param.id,type:3},function (data) {
                num1 = data;
                $("#likeBtn").html(num1)
            })

            if (param.id) {
                //获取作用域中的用户信息
                var user = JSON.parse(sessionStorage.getItem("user"));

                if(user){
                    //查询当前用户的收藏情况
                    $.get("/news/collectionCount", {travelId: param.id, userId: user.id,type:3}, function (data) {
                        if (data) {
                            $("#collectBtn").css("color", "orange").removeClass("fa-star-o").addClass("fa-star")
                        } else {
                            $("#collectBtn").css("color", "black").removeClass("fa-star").addClass("fa-star-o")
                        }
                    })
                }

                $("#collectBtn").click(function () {
                    if (user!=null) {
                        $.get("/news/collectionCount", {travelId: param.id, userId: user.id,type:3}, function (data) {
                            if (data) {
                                $.get("/news/" + param.id, function (data) {
                                    $.ajax({
                                        type: "delete",
                                        url: "/news/" +param.id,
                                        success: function () {
                                            $("#collectBtn").css("color", "black").removeClass("fa-star").addClass("fa-star-o")
                                        }
                                    });
                                });
                                state = false;
                            } else {
                                $.post("/news/collection", {travelId: param.id, userId: user.id,type:3}, function (data) {
                                    if (data.success) {
                                        $("#collectBtn").css("color", "orange").removeClass("fa-star-o").addClass("fa-star")
                                    }
                                })
                                state = true;
                            }
                            if (state) {
                                num++;
                            }else{
                                num--;
                            }
                            $("#collectBtn").html(num);
                        })

                    }else {
                        //没有登录,跳转到登录页面
                        $(document).dialog({
                            autoClose: 1200,
                            content: '1.5秒后跳转到登录页面'
                        });

                        setTimeout(function () {
                            window.location.href = "/login.html"
                        },1500);

                    }
                })

                //点赞
                if (user) {
                    //用户登录且有点赞过显示红色
                    $.get("/praises", {travelId: param.id, userId: user.id,type:3}, function (data) {
                        if (data) {
                            $("#likeBtn").css("color", "orange").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up")
                        }
                    })
                } else {
                    //没有登录时该图标为黑色
                    $("#likeBtn").css("color", "").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                }

                //点赞或取消点赞
                $("#likeBtn").click(function () {
                    if (user != null) {
                        //判断该用户是否有点赞
                        $.get("/praises", {travelId: param.id, userId: user.id,type:3}, function (data) {
                            if (data) {
                                $.ajax({
                                    type: "delete",
                                    url: "/praises/" + param.id+"/news",
                                    success: function () {
                                        $("#likeBtn").css("color", "").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                                    }
                                });
                                state1 = false;
                            }else {
                                $.post("/praises", {travelId: param.id, userId: user.id,type:3}, function (data) {
                                    if (data.success) {
                                        $("#likeBtn").css("color", "orange").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up")
                                    }
                                })
                                state1 = true;
                            }
                            if (state1) {
                                num1++;
                            }else{
                                num1--;
                            }
                            $("#likeBtn").html(num1);
                        })

                    } else {
                        //没有登录,跳转到登录页面
                        $(document).dialog({
                            autoClose: 1200,
                            content: '1.5秒后跳转到登录页面'
                        });

                        setTimeout(function () {
                            window.location.href = "/login.html"
                        },1500);
                    }
                })
            }
        })
    </script>

</head>

<body>
<div class="newsDetail">
    <div>
        <a href="javascript:history.go(-1);" style="position: absolute;top: 10px;left: 22px;">
            <span><i class="fa fa-chevron-left fa-2x" style="color: white;"></i></span>
        </a>
        <img render-src="coverUrl">

        <div class="container">
            <div class="row typeRow">
                <div class="col-4">
                    <span class="type">美图美文</span>
                </div>
                <div class="col-2">
                </div>
                <div class="col-2">
                    <span>By</span>
                </div>
                <div class="col-4">
                    <span class="type">骡窝官方</span>
                </div>
            </div>

            <div class="detail">
                <h2 class="title" render-html="title"></h2>
                <div class="content" render-html="newsContent.content"></div>
            </div>

        </div>
        <div class="container d-flex">
            <!-- 点赞 -->
            <div class="p-2  flex-fill">
                <i class="fa fa-thumbs-o-up"  id="likeBtn"> </i>
            </div>

            <div class="p-2  flex-fill">
                <i class="fa  fa-commenting-o"  id="commentBtn"> </i>
            </div>

            <!-- 收藏 -->
            <div class="p-2  flex-fill">
                <i class="fa fa-star-o" id="collectBtn"> </i>
            </div>
        </div>
    </div>
</div>


</body>

</html>